package csu.webcourse.mypetstore.web.servlet;

import com.alibaba.fastjson.JSON;
import csu.webcourse.mypetstore.domain.Product;
import csu.webcourse.mypetstore.service.CategoryService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ShowCategoryDetails extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String categoryId = req.getParameter("categoryId");
        CategoryService categoryService = new CategoryService();
        List<Product> productList = categoryService.getProductListByCategory(categoryId);

        String productStringList = JSON.toJSONString(productList);

        resp.setContentType("text/json");
        PrintWriter out = resp.getWriter();
        out.println(productStringList);
    }
}
